package com.zengjianjun.oss.exception;

import cn.hutool.core.text.CharPool;
import com.zengjianjun.oss.enums.OssPlatformEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * 消息推送处理异常类
 *
 * @author zengjianjun
 */
public class OssException extends RuntimeException {

    public OssException(String message) {
        super(message);
    }

    public OssException(Throwable throwable) {
        super(throwable);
    }

    public OssException(String message, Throwable throwable) {
        super(message, throwable);
    }

    /**
     * 返回一个新的异常，统一构建，方便统一处理
     *
     * @param msg    消息
     * @param t      异常
     * @param params 参数
     * @return 自定义异常
     */
    public static OssException build(String msg, Throwable t, Object... params) {
        return new OssException(String.format(msg, params), t);
    }

    /**
     * 重载
     *
     * @param msg    消息
     * @param params 参数
     * @return 自定义异常
     */
    public static OssException build(OssPlatformEnum platformEnum, String msg, Object... params) {
        if (StringUtils.contains(msg, "{}")) {
            msg = StringUtils.replace(msg, "{}", "%s");
        }
        return new OssException(CharPool.BRACKET_START + platformEnum.name() + CharPool.BRACKET_END + String.format(msg, params));
    }

    public static OssException build(String msg, Object... params) {
        if (StringUtils.contains(msg, "{}")) {
            msg = StringUtils.replace(msg, "{}", "%s");
        }
        return new OssException(String.format(msg, params));
    }

    /**
     * 重载
     *
     * @param t 异常
     * @return 自定义异常
     */
    public static OssException build(Throwable t) {
        return new OssException(t);
    }
}
